﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Utility
{
    public static class Enum<T>
    {
        public static T Parse(string enumName)
        {
            if (!string.IsNullOrEmpty(enumName))
                return (T)Enum.Parse(typeof(T), enumName, true);
            return default(T);
        }

        public static bool TryParse(string enumName, out T t)
        {
            try
            {
                t = (T)Enum.Parse(typeof(T), enumName, true);
                return true;
            }
            catch { }
            t = default(T);
            return false;
        }

        public static T FromInt(int i)
        {
            return (T) Enum.ToObject(typeof (T), i);
        }

        public static IList<T> ToList()
        {
            return ToEnumerable().ToList();
        }

        public static IEnumerable<T> ToEnumerable()
        {
            return Enum.GetValues(typeof(T)).OfType<T>();
        }

        public static int Count
        {
            get { return Enum.GetValues(typeof(T)).Length; }
        }

        public static int Index(T value)
        {
            return (int)((object)value);
        }
    }
}
